package com.itranswarp.learnjava.listener;


import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import jakarta.servlet.annotation.WebListener;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.itranswarp.learnjava.utils.DBUtil;


@WebListener
public class IndexListener implements ServletContextListener {
    static final String jdbcUrl = "jdbc:mysql://localhost/level1?allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=utf8";
    static final String jdbcUsername = "root";
    static final String jdbcPassword = "111111";
    static Connection con;
    // 在此初始化WebApp,例如打开数据库连接池等:
    public void contextInitialized(ServletContextEvent sce) {
        DBUtil.init(jdbcUrl, jdbcUsername, jdbcPassword);
        System.out.println("WebApp initialized.");
    }

    // 在此清理WebApp,例如关闭数据库连接池等:
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("WebApp destroyed.");
    }
}
